package com.jinwei.qunguanjia.framework.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.jinwei.qunguanjia.common.utils.DateUtils;
import com.jinwei.qunguanjia.common.utils.SystemContextUtils;
import com.jinwei.qunguanjia.common.utils.TenantContextUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

@Component
public class BaseEntityMetaObjectHandler implements MetaObjectHandler {

    private final static String CREATE_TIME = "createTime";
    private final static String CREATE_BY = "createBy";
    private final static String CREATE_BY_ID = "createById";
    private final static String UPDATE_TIME = "updateTime";
    private final static String UPDATE_BY = "updateBy";
    private final static String UPDATE_BY_ID = "updateById";
    //private final static String TENANT_ID = "tenant_id";


    private void setMeta(MetaObject metaObject, String field, Object defaultValue){
        if(metaObject.hasGetter(field)){
            Object value = metaObject.getValue(field);
            if(null != value){
                this.setFieldValByName(field, value, metaObject);
            }else {
                this.setFieldValByName(field, defaultValue, metaObject);
            }
        }
    }

    private void setCreateTime(MetaObject metaObject){
        setMeta(metaObject, CREATE_TIME, DateUtils.getDateTime());
    }

    private void setCreateBy(MetaObject metaObject){
        Object createBy = SystemContextUtils.getUserName();
        if(null == SystemContextUtils.getCurrentSystemUser()){
            return ;
        }
        setMeta(metaObject, CREATE_BY, createBy);
    }

    private void setCreateById(MetaObject metaObject){
        if(null == SystemContextUtils.getCurrentSystemUser()){
            return ;
        }
        setMeta(metaObject, CREATE_BY_ID, SystemContextUtils.getUserId());
    }

//    private void setTenantId(MetaObject metaObject){
//        Object tenantId = TenantContextUtils.getCurrentTenantId();
//        if(null == tenantId){
//            return ;
//        }
//        setMeta(metaObject, TENANT_ID, tenantId);
//    }

    private void setUpdateTime(MetaObject metaObject){
        setMeta(metaObject, UPDATE_TIME, DateUtils.getDateTime());
    }

    private void setUpdateBy(MetaObject metaObject){
        if(null == SystemContextUtils.getCurrentSystemUser()){
            return ;
        }
        setMeta(metaObject, UPDATE_BY, SystemContextUtils.getUserName());
    }

    private void setUpdateById(MetaObject metaObject){
        if(null == SystemContextUtils.getCurrentSystemUser()){
            return ;
        }
        setMeta(metaObject, UPDATE_BY_ID, SystemContextUtils.getUserId());
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        setCreateTime(metaObject);
        setCreateBy(metaObject);
        setCreateById(metaObject);
        //setTenantId(metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        setUpdateTime(metaObject);
        setUpdateBy(metaObject);
        setUpdateById(metaObject);
    }
}
